package server.session;

import server.Container;
import server.Logger;
import server.Manager;
import server.Session;

import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

public class ManagerBase implements Manager {
    protected static final String DEFAULT_ALGORITHM = "MD5";
    protected static final int SESSION_ID_BYTES = 16;
    protected static String name = "ManagerBase";


    protected String algorithm = DEFAULT_ALGORITHM;
    protected Container container;
    protected int debug = 0;
    private static final String info = "ManagerBase/1.0";

    //最长活动时间,单位为秒
    protected int maxInactiveInterval = 60;
    protected HashMap<String, Session> sessions = new HashMap<>();

    void log(String message) {
        Logger logger = null;
        if (container != null)
            logger = container.getLogger();
        if (logger != null)
            logger.log(getName() + "[" + container.getName() + "]: " + message);
        else {
            String containerName = null;
            if (container != null)
                containerName = container.getName();
            System.out.println(getName() + "[" + containerName + "]: " + message);
        }
    }

    void log(String message, Throwable throwable) {
        Logger logger = null;
        if (container != null)
            logger = container.getLogger();
        if (logger != null)
            logger.log(getName() + "[" + container.getName() + "] "
                    + message, throwable);
        else {
            String containerName = null;
            if (container != null)
                containerName = container.getName();
            System.out.println(getName() + "[" + containerName
                    + "]: " + message);
            throwable.printStackTrace(System.out);
        }

    }
    //getter----------------------------------------------------------------------
    public String getAlgorithm() {
        return this.algorithm;
    }

    public int getDebug() {
        return this.debug;
    }

    public String getName() {
        return name;
    }
    //setter----------------------------------------------------------------------
    public void setAlgorithm(String algorithm) {
        this.algorithm = algorithm;
    }

    public void setDebug(int debug) {
        this.debug = debug;
    }
    //实现Manager接口----------------------------------------------------------------

    @Override
    public Container getContainer() {
        return container;
    }

    @Override
    public void setContainer(Container container) {
        this.container = container;
    }

    @Override
    public String getInfo() {
        return this.info;
    }

    @Override
    public int getMaxInactiveInterval() {
        return this.maxInactiveInterval;
    }

    @Override
    public void setMaxInactiveInterval(int interval) {
        this.maxInactiveInterval = interval;
    }

    @Override
    public void add(Session session) {
        synchronized (sessions) {
            sessions.put(session.getId(), session);
        }
    }

    @Override
    public Session findSession(String id) throws IOException {
        if (id == null)
            return null;
        synchronized (sessions) {
            return sessions.get(id);
        }
    }

    @Override
    public Session[] findSessions() {
        Session results[] = null;
        synchronized (sessions) {
            results = new Session[sessions.size()];
            results = sessions.values().toArray(results);
        }
        return results;
    }

    @Override
    public void remove(Session session) {
        synchronized (sessions) {
            sessions.remove(session.getId());
        }
    }

    @Override
    public Session createSession() {
        Session session = new StandardSession(this);
        session.setNew(true);
        session.setValid(true);
        session.setCreationTime(System.currentTimeMillis());
        session.setMaxInactiveInterval(this.maxInactiveInterval);
        session.setId(UUID.randomUUID().toString());
        return session;
    }


}
